﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicalLog.Domain.Entities;

namespace MedicalLog.Domain.Repositories
{
    public class VitalSignListRepository
    {
        MedicalLogEntities _dbMedicalLog;

        public VitalSignListRepository()
        {
            _dbMedicalLog = new MedicalLogEntities();
        }

        /// <summary>
        /// This methods adds a vital sign list to the database
        /// </summary>
        /// <param name="newVitalSignList"></param>
        public void AddVitalSignList(VitalSingList newVitalSignList)
        {
            _dbMedicalLog.AddToVitalSingLists(newVitalSignList);
            _dbMedicalLog.SaveChanges();
        }
        
        /// <summary>
        /// This method deletes a vital sign list from the database
        /// </summary>
        /// <param name="pId"></param>
        public void DeleteVitalSignList(int pId)
        {
            var vitalSignList = _dbMedicalLog.VitalSingLists.Where(x => x.Id == pId).First();
            if (vitalSignList != null)
            {
                _dbMedicalLog.VitalSingLists.DeleteObject(vitalSignList);
                _dbMedicalLog.SaveChanges();
            }
        }
        
        /// <summary>
        /// This method returns a vital sign list with the selected id from the database
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public VitalSingList DetailVitalSignList(int pId)
        {
            return _dbMedicalLog.VitalSingLists.Where(x => x.Id == pId).First();
        }
        
        /// <summary>
        /// This method updates a vital sign list
        /// </summary>
        /// <param name="oldVitalSignList"></param>
        public void UpdateVitalSignList(VitalSingList oldVitalSignList)
        {
            _dbMedicalLog.VitalSingLists.FirstOrDefault(x => x.Id == oldVitalSignList.Id);
            _dbMedicalLog.VitalSingLists.ApplyCurrentValues(oldVitalSignList);
            _dbMedicalLog.SaveChanges();
        }
        
        /// <summary>
        /// This method get a Vital Sign List with the selected name
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public VitalSingList getSignList(string pName)
        {
            return _dbMedicalLog.VitalSingLists.Where(x => x.VitalSignName == pName).First();
        }
    }
}
